/**
 * Licensed to Open-Ones Group under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Open-Ones Group licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a
 * copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package mks.webshop;

import java.util.List;
import java.util.logging.Logger;

import mks.webshop.common.AC;
import mks.webshop.dao.DaoManager;
import mks.webshop.dao.IMasterDao;
import mks.webshop.entity.BasePanel;
import mks.webshop.entity.Category;
import mks.webshop.entity.Counter;
import mks.webshop.entity.Logo;
import mks.webshop.entity.Notice;
import mks.webshop.entity.Order;
import mks.webshop.entity.OrderDetail;
import mks.webshop.entity.PanelType;
import mks.webshop.entity.Product;
import mks.webshop.entity.User;
import mks.webshop.entity.Food;

/**
 * @author thachle
 */
public class Workspace {
    static final Logger LOG = Logger.getLogger("Workspace");
    
    /** Language code. Supporting: en, vi. */
    private String lang;
    
    /** Master Data Access Object . */
    private IMasterDao iMstDao = DaoManager.getMasterDaoInst();

	public List<Product> inverseList(List<Product> listPro){
		return iMstDao.inverseList(listPro);
	};

    /**
     * Create default instance of Workspace by Vietnamese.
     */
    public Workspace() {
        this.lang = AC.VN;
        iMstDao.setLangCd(this.lang);
    }
    
    /**
     * Create an instance of Workspace with given language.
     * @param langCd
     */
    public int getAmountSubcategory (Category cat){
    	return iMstDao.getAmountSubcategory(cat.getId(), cat.getLevel());
    }
    
    public int getAmountSubproduct (Category cat){
    	return iMstDao.getAmountSubproduct(cat.getId());
    }
    /**
     * Create an instance of Workspace with given language.
     * @param langCd
     */
    public Workspace(String langCd) {
        this.lang = ((langCd == null) || langCd.isEmpty()) ? AC.VN : langCd;
        iMstDao.setLangCd(this.lang);
    }

    /**
     * Get all product categories of level 1.
     * @return
     */
    public List<Category> getCategoryList() {
        return iMstDao.getCategoryList();
    }
    
    /**
     * Get all product categories of.
     * @return
     */
    public List<Category> getAllCategoryList(){
    	return iMstDao.getAllCategoryList();
    }
    	
    /**
     * Get category by id.
     * @return
     
    public Category getCategoryById(Category cat){
        return iMstDao.getCategoryById(cat.getId());
    }
    */
    public Category getCategoryById(String id){
        return iMstDao.getCategoryById(id);
    }
    /**
     * Save product category.
     * @param cat
     * @return
     */
    public boolean save(Category cat) {
        return iMstDao.saveCat(cat);
    }
    
    public boolean save(Logo logo){
    	return iMstDao.saveLogo(logo);
    }
    
    /**
     * Get all sub categories of given category id (subCategory's level greater than parentCategory 1).
     * @param catId identifier of parent category
     * @return list of sub categories. empty list of the given category is leaf.
     */
    public List<Category> getSubCategory(Category cat) {
        return iMstDao.getSubCategoryList(cat.getId(), cat.getLevel());
    }
    
    /**
     * Get all sub categories of given category id.
     * @param catId identifier of parent category
     * @return list of sub categories. empty list of the given category is leaf.
     */
    public List<Category> getAllSubCategoryList(Category cat) {
    	return iMstDao.getAllSubCategoryList(cat.getId());
    }
    
    public List<Category> getParentCategory(Category cat){
    	return iMstDao.getParentCategoryList(cat.getId(), cat.getLevel());
    }
    
    /**
     * Lấy danh sách các product con có parentId là id của category cha
     */
    public List<Product> getSubProductList (String catId){
    	return iMstDao.getSubProductList(catId);
    }

    public boolean deleteCatByKey(Long key) {
        return iMstDao.deleteCatByKey(key);
    }
    
    public boolean deleteProByKey(Long key) {
        return iMstDao.deleteProByKey(key);
    }
    public boolean deleteOrderByKey(Long key){
    	return iMstDao.deleteOrderByKey(key);
    }
    
//    public boolean deleteSubProByCat(Category cat) {
//    	return iMstDao.deleteSubProByCat(cat);
//    }
//    
//    public boolean deleteSubCatByCat(Category cat) {
//    	return iMstDao.deleteSubCatByCat(cat);
//    }
//    
    /**
     * Get all products of given category.
     * @param catId category identifier
     * @return
     */
    public List<Product> getProductList() {
        return iMstDao.getProductList();
    }
    
    public Product getProductById(String id){
    	return iMstDao.getProductById(id);
    }
    public boolean save(Product pro) {
        return iMstDao.saveProduct(pro);
    }
    
    public boolean saveProductImage(Product pro){
    	return iMstDao.saveProductImage(pro);
    }
    
    public boolean isExistedCat(String id) {
    	return iMstDao.isExistedCat(id);
    }
    
    public boolean isExistedPro(String id) {
    	return iMstDao.isExistedPro(id);
    }
    public boolean savePanel(BasePanel panel) {
        return iMstDao.savePanel(panel);
    }
        
    public BasePanel getPanel(PanelType type) {
        return iMstDao.getPanel(type);
    }
    
    public Logo getLogo(){
    	return iMstDao.getLogo();
    }
    
    public List<Product> getAllSaleOffProductList(){
    	return iMstDao.getAllSaleOffProductList();
    } 
    
    public List<Product> getAllProductList(){
    	return iMstDao.getAllProductList();
    }
    
    public List<Product> getProductWithQuantity(List<Product> modList, int quantity){
    	return iMstDao.getProductWithQuantity(modList, quantity);
    }
    
    public List<Product> getSubProductListSortingName(String catid){
    	return iMstDao.getSubProductListSortingName(catid);
    }

	public boolean save(Notice note) {
		return iMstDao.saveNotice(note);
	}
	public Notice getNoticeById(String id) {
		return iMstDao.getNoticeById(id);
	}

	public boolean deleteNoticeByKey(Long key) {
		return iMstDao.deleteNoticeByKey(key);
	}
	
	public List<Notice> getAllNoticeList(){
		return iMstDao.getAllNoticeList();
	}
	
	public List<Notice> getNoticeWithQuantity(List<Notice> modNoticeList, int quantity){
		return iMstDao.getNoticeWithQuantity(modNoticeList, quantity);
	}

	public List<Notice> inverseNoticeList(List<Notice> modNoticeList) {
		return iMstDao.inverseNoticeList(modNoticeList);
	}
	
	public boolean save(User user) {
        return iMstDao.saveUser(user);
    }
	///Service for Food
	public boolean saveFood(Food food){
		return iMstDao.saveFood(food);
	}
	
	public boolean deleteFood(long id){
		return iMstDao.deleteFood(id);
	}
	
	public Food getFoodByCode(String code){
		return iMstDao.getFoodByCode(code);
	}
	
	public List<Food> getAllFoodList(){
		return iMstDao.getAllFoodList();
	}
	
	public boolean saveFoodImage(Food food){
    	return iMstDao.saveFoodImage(food);
    }
	
	public List<Food> getFoodWithQuantity(List<Food> modFoodList, int quantity){
		return iMstDao.getFoodWithQuantity(modFoodList, quantity);
	}

	public List<Food> inverseFoodList(List<Food> modFoodList) {
		return iMstDao.inverseFoodList(modFoodList);
	}

	public List<Product> getListProductByName(String name) {
		return iMstDao.getListProductByName(name);
	}
	
	public User getUserById(String id){
		return iMstDao.getUserById(id);
	}
	
	///Service for Order and OrderDetail
	public boolean saveOrder(Order order){
		return iMstDao.saveOrder(order);
	}
	
	public boolean setIsDone(Order order, boolean isDone){
		return iMstDao.setIsDone(order, isDone);
	}
	
	public boolean setProductIsSpecial(Product pro, int isSpecial){
		return iMstDao.setProductIsSpecial(pro, isSpecial);
	}
	
	public Order getOrderByCode(String code){
		return iMstDao.getOrderByCode(code);
	}
	
	public long getMaxOrderKey(){
		return iMstDao.getMaxOrderKey();
	}
	
	public List<Order> getAllOrder(){
		return iMstDao.getAllOrder();
	}
	
	public List<Order> getOrdersByState(boolean isDone){
		return iMstDao.getOrdersByState(isDone);
	}
	
	public boolean saveOrderDetail(OrderDetail detail){
		return iMstDao.saveOrderDetail(detail);
	}
	
	public List<OrderDetail> getOrderDetailByOrderCode(String code){
		return iMstDao.getOrderDetailByOrderCode(code);
	}
	
	public long computeBasePrice(List<OrderDetail> list){
		return iMstDao.computeBasePrice(list);
	}
	
	//login
	public boolean checkLogin(String username, String password){
		return iMstDao.checkLogin(username, password);
	}
	
	public List<User> getAllUser(){
		return iMstDao.getAllUser();
	}

	public boolean deleteUserByKey(Long key) {
		return iMstDao.deleteUserByKey(key);
	}
	
	//Counter
	public boolean saveCounter(Counter counter, int increament){
		return iMstDao.save(counter, increament);
	}
	
	public Counter getCounter(){
		return iMstDao.getCounter();
	}
}
